<?php

namespace App\Http\Controllers\Auth;

use App\models\user\SteamUser;
use App\models\user\User;
use App\other\Notification\SendMail;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Invisnik\LaravelSteamAuth\SteamAuth;
use Session;

class AuthSteamController extends Controller
{
    private $steam;

    public function __construct(SteamAuth $steam){
        $this->steam = $steam;
    }
    public function steamLogin(){
        if (Auth::check() && !empty(Auth::user()->steam)){
            return redirect('/');
        }
        return $this->steam->redirect();
    }

    public function steamRegister(){
        if (Auth::check()){
            return redirect('/');
        }
        if (Session::has('steam') == false){
            return redirect('/steam/login');
        }
        $steam = Session::get('steam');
        return view('auth.register', compact('steam'));
    }

    public function steamVerify(){
        if (Auth::check()&& !empty(Auth::user()->steam)){
            return redirect('/');
        }
        if ($this->steam->validate()) {
            $info = $this->steam->getUserInfo();
            $steam = SteamUser::getSteamUserBySteamID64($info['steamID64'] );
            if (!empty($steam)){
                $user = $steam->user;
                $isTrue = $this->verifiedUser($user);
                if($isTrue != true){
                    return $isTrue;
                }
                Auth::loginUsingId($user->id, true);
                return redirect('/');
            }else {
                $array = $this->ckSteamInfo($info);
                if (Auth::user()){
                    $this->bindSteam($array);
                    return redirect('/');
                }
                Session::forget('steam');
                Session::push('steam', $array);
                return redirect('/steam/register');
            }
        }
        return view('errors.404');
    }

    /**
     *   验证用户是否可以被登录
     * @param User $user
     * @return bool|\Illuminate\Http\RedirectResponse
     */
    private function verifiedUser(User $user){
        if ($user->verified == 0){
            SendMail::sendRegisterEmail($user);
            return redirect('/login')->with(['_error' => '邮箱未激活,已发送激活邮件']);
        }
        if ($user->is_ban == 1 && $user->ban_time > time()){
            return redirect('/login')->with(['_error' => '你被封禁至:'.date('Y/m/d H:i:s', $user->ban_time)]);
        }elseif ($user->is_ban == -1){
            return redirect('/login')->with(['_error' => '你已经被永久封禁']);
        }else{
            $user->is_ban = 0;
            $user->save();
        }
        return true;
    }

    /**
     * 重新绑定steam用户
     * @param array $info
     */
    private function bindSteam($list){
        $steam          = new SteamUser();
        $list['uid']    = Auth::id();
        $steam->setRawAttributes($list);
        $steam->save();
    }

    /**
     * 把steam的详情转换为array
     * @param $info
     * @return array|mixed
     */
    private function ckSteamInfo($list){
        $info = (array)$list;
        $array =  Array();
        foreach ($info as $item){
            $array = $item;
        }
        return $array;
    }
}
